package com.csyf.manager.controller;

import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.exception.Exception;
import com.csyf.manager.service.SysChannelBiz;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.service.impl.RedisService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/25 0025.
 */

@Controller
@RequestMapping(value = "sys")
public class SystemController {

    @Autowired
    private SysMerchBiz sysMerchBiz;

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private SysChannelBiz sysChannelBiz;

    @Autowired
    private RedisService redisService;

    @GetMapping(value = "login")
    public String loginview() {
        return "login";
    }


    @GetMapping(value = "pwdmodel")
    public String pwdmodel() {
        return "view/password";
    }


    @GetMapping(value = "downloadview")
    public String downloadview() {
        return "view/download";
    }


    @GetMapping(value = "agentview")
    public String agentview(Model model) {

        SysMerchant userInfo = getUserInfo();
        model.addAttribute("user",userInfo);
        return "view/userInfo";
    }


    @ResponseBody
    @PostMapping(value = "modifypwd")
    public ResultEntity modifypwd(String oldpwd, String newpwd) {
        SysMerchant userInfo = getUserInfo();
        try {
            sysMerchBiz.modifyPwd(userInfo.getId(), oldpwd, newpwd);
            return ResultUtli.DEFAULT_SUCCESS("修改成功", null);
        } catch (Exception e) {
            return ResultUtli.ERRO(100, e.getMessage(), null);
        }
    }

    @PostMapping(value = "user_login")
    public String login(Model model, String username, String pwd) {
        try {
            SysMerchant sysAgent = sysMerchBiz.agentLogin(username, pwd);
            httpSession.setAttribute("login", sysAgent);
            return "redirect:/sys/";
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
            return "login";
        }
    }

    @GetMapping(value = "/")
    public String main(Model model) {
        return "index";
    }


    public String handleHtml(Map<String,Object> channelMap) {
        StringBuffer sbf = new StringBuffer("<div class=\'ibox-content\'>");
        if (null != channelMap) {
            Iterator<Map.Entry<String,Object>> iter = channelMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String,Object> entry = iter.next();
                sbf.append("<td>"+entry.getKey()+"：</td><td>"+entry.getValue()+"</td><br/>\n");
            }
        }
        sbf.append("</div>");
        return sbf.toString();
    }


    @GetMapping(value = "loginout")
    public String loginout() {
        httpSession.invalidate();
        return "redirect:/sys/login";
    }

    private SysMerchant getUserInfo() {
        return (SysMerchant) httpSession.getAttribute("login");
    }

}
